package com.alameda.exception;

import com.alameda.result.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 业务异常类
 *
 * @author Lv
 * @date 2024/2/19
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class BusinessException extends RuntimeException {
    /**
     * 错误枚举值
     */
    private ResultEnum resultEnum;

    /**
     * 详细错误信息
     */
    private String message;

    /**
     * 错误枚举对象
     *
     * @param resultEnum 错误枚举对象
     */
    public static void throwException(ResultEnum resultEnum) {
        throw new BusinessException(resultEnum);
    }

    /**
     * 抛出业务异常 携带详细错误信息
     *
     * @param resultEnum   错误枚举对象
     * @param errorMessage 详细错误信息
     */
    public static void throwException(ResultEnum resultEnum, String errorMessage) {
        throw new BusinessException(resultEnum, errorMessage);
    }

    /**
     * 抛出业务异常 默认错误信息携带详细错误信息
     *
     * @param errorMessage 详细错误信息
     */
    public static void throwException(String errorMessage) {
        throw new BusinessException(ResultEnum.FAILED, errorMessage);
    }

    /**
     * 抛出业务异常
     *
     * @param throwable 错误堆栈
     */
    public static void throwException(Throwable throwable) {
        Exception exception = (throwable instanceof BusinessException)
                ? (BusinessException) throwable
                : (RuntimeException) throwable;

        BusinessException.throwException(exception.getMessage());
    }

    /**
     * 构造函数 防止构造失败
     *
     * @param resultEnum 错误枚举对象
     */
    public BusinessException(ResultEnum resultEnum) {
        this.resultEnum = resultEnum;
    }

}
